package com.nmgs.util;

import com.nmgs.config.JaxWsDynamicClientFactory;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.json.JSONObject;

import java.util.Map;

public class WebServiceUtil {

    public static String toWebService(String path, String WebServceName) {
        try {
            // 创建动态客户端
            JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
            Client client = dcf.createClient(path);
            // 需要密码的情况需要加上用户名和密码
            // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
            Object[] objects = new Object[0];
            //json的形式
            objects = client.invoke(WebServceName, new Object[]{""});
            if (objects.length > 0) {
                return objects[0].toString();
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String toWebService(String path, String WebServceName, Map<String, Object> params) {
        try {
            // 创建动态客户端
            JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
            Client client = dcf.createClient(path);
            // 需要密码的情况需要加上用户名和密码
            // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
            Object[] objects = new Object[0];
            //json的形式
            JSONObject jsonMap = new JSONObject(params);
            objects = client.invoke(WebServceName, new Object[]{jsonMap.toString()});
            if (objects.length > 0) {
                return objects[0].toString();
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String toWebService(String path, String WebServceName, String methodName) {
        try {
            // 创建动态客户端
            JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
            Client client = dcf.createClient(path);
            // 需要密码的情况需要加上用户名和密码
            // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
            Object[] objects = new Object[0];
            //json的形式
            objects = client.invoke(WebServceName, new Object[]{methodName, ""});
            if (objects.length > 0) {
                return objects[0].toString();
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String toWebService(String path, String WebServceName, String methodName, Map<String, Object> params) {
        try {
            // 创建动态客户端
            JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
            Client client = dcf.createClient(path);
            // 需要密码的情况需要加上用户名和密码
            // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
            Object[] objects = new Object[0];
            //json的形式
            JSONObject jsonMap = new JSONObject(params);
            objects = client.invoke(WebServceName, new Object[]{methodName, jsonMap.toString()});
            if (objects.length > 0) {
                return objects[0].toString();
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Map<String, Object> getResult(String obj) {
        if (obj == null || obj.equals("")) {
            return null;
        }
        Map<String, Object> map = com.alibaba.fastjson.JSONObject.parseObject(obj);
        if (map.containsKey("resultData")) {
            if (map.get("resultData") != null && !map.get("resultData").toString().equals("")) {
                try {
                    return com.alibaba.fastjson.JSONObject.parseObject(map.get("resultData").toString());
                } catch (Exception e) {
                    e.printStackTrace();
                    LogUtil.error(map.get("resultData").toString() + "转map失败！", WebServiceUtil.class);
                    return map;
                }
            } else {
                return null;
            }
        } else {
            return map;
        }
    }
    public static String getServiceData(String path, String methodName) {
        // 创建动态客户端
        int CXF_CLIENT_CONNECT_TIMEOUT = 2 * 60000;
        int CXF_CLIENT_RECEIVE_TIMEOUT = 2 * 60000;
        org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory dcf = org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(path);
        HTTPConduit conduit = (HTTPConduit) client.getConduit();
        HTTPClientPolicy policy = new HTTPClientPolicy();
        policy.setConnectionTimeout(CXF_CLIENT_CONNECT_TIMEOUT);
        policy.setReceiveTimeout(CXF_CLIENT_RECEIVE_TIMEOUT);
        conduit.setClient(policy);
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects = new Object[]{"", ""};
        //json的形式
        //JSONObject jsonMap = new JSONObject(params);
        try {
            objects = client.invoke(methodName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (objects.length > 0) {
            try {
                objects[0] = StringEscapeUtils.unescapeJava(objects[0].toString()).replace("\"{", "{").replace("}\"", "}").replace("\\", "/");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return objects[0].toString();
        } else {
            return null;
        }
    }
    public static String getServiceData(String path, String methodName, Map<String, Object> paramsMap) {
        Object[] object = null;
        // 创建动态客户端
        String params = new JSONObject(paramsMap).toString();
        int CXF_CLIENT_CONNECT_TIMEOUT = 2 * 60000;
        String timeOut = PropertiesUtil.getValue("CxfClientReceiveTimeOut");
        if (timeOut == null || timeOut.equals("")) {
            timeOut = 60 + "";
        }
        int CXF_CLIENT_RECEIVE_TIMEOUT = Integer.parseInt(timeOut) * 60000;
        org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory dcf = org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(path);
        HTTPConduit conduit = (HTTPConduit) client.getConduit();
        HTTPClientPolicy policy = new HTTPClientPolicy();
        policy.setConnectionTimeout(CXF_CLIENT_CONNECT_TIMEOUT);
        policy.setReceiveTimeout(CXF_CLIENT_RECEIVE_TIMEOUT);
        conduit.setClient(policy);
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects = new Object[0];
        //json的形式
        //JSONObject jsonMap = new JSONObject(params);
        Object[] aaa = new Object[]{params};
        try {
            objects = client.invoke(methodName, aaa);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (objects.length > 0) {
            try {
                objects[0] = StringEscapeUtils.unescapeJava(objects[0].toString()).replace("\"{", "{").replace("}\"", "}").replace("\\", "/");
                return objects[0].toString();
            } catch (Exception e) {
                return null;
            }
        } else {
            return null;
        }
    }
}
